﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DLULearningNetworkModel;
using DLULearningNetwork.Libs;
using DLULearningNetwork.ActionFilterAttributes;

namespace DLULearningNetwork.Controllers
{
    //Friend AccountID la nguoi ket ban, FriendAccountID la nguoi duoc moi ket ban
    public class FriendController : Controller
    {
        [RequireUserLogin()]
        public ActionResult Index()
        {
            return View();
        }
        
        [RequireUserLogin()]
        public ActionResult Search()
        {
            return View();
        }
        
        [HttpPost]
        [RequireUserLogin()]
        public ActionResult Search(string firstName, string lastName, int page = 1)
        {
            int totalItems = 0;
            List<Account> lstAccountResult = Account.SearchByName(RequireUserLogin.LoginUser.AccountID_u, firstName, lastName, page, Constants.DEFAULT_NUMBER_OF_ITEMS_PERPAGE, ref totalItems);
            ViewBag.TotalItems = totalItems;
            ViewBag.CurrentPage = page;
            return PartialView("SearchResult", lstAccountResult);
        }

        [RequireUserLogin()]
        public ActionResult MyFriend()
        {
            int totalItems = 0;
            ViewBag.LstFriend = Friend.SelectFriendOfAnAccount(RequireUserLogin.LoginUser.AccountID_u, 1, Constants.DEFAULT_NUMBER_OF_ITEMS_PERPAGE, ref totalItems);
            ViewBag.TotalItems = totalItems;
            ViewBag.CurrentPage = 1;
            return View();
        }
        
        [HttpPost]
        [RequireUserLogin()]
        public ActionResult MyFriend(int page)
        {
            int totalItems = 0;
            ViewBag.LstFriend = Friend.SelectFriendOfAnAccount(RequireUserLogin.LoginUser.AccountID_u, page, Constants.DEFAULT_NUMBER_OF_ITEMS_PERPAGE, ref totalItems);
            ViewBag.TotalItems = totalItems;
            ViewBag.CurrentPage = page;
            return PartialView("ListMyFriend", ViewBag.LstFriend);
        }

        // Chi nguoi duoc moi ket ban moi co quyen accept, accountID la ID cua nguoi duoc moi ket ban
        [RequireUserLogin()]
        public string AcceptFriendRequest(Guid friendID)
        {
            Account currentUser = RequireUserLogin.LoginUser;
            //Friend friendRelation = Friend.Single(Guid.Parse(friendID));
            ////neu khong co moi quan he hoac nguoi dung hien tai khong phai nguoi duoc moi
            //if(friendRelation == null || friendRelation.FriendAccountID_u != currentUser.AccountID_u)
            //{
            //    return "unsuccess";
            //}
            
            int kq = Friend.Accept(friendID,currentUser.AccountID_u);
            if (kq == 1)
            {
                
                //Tao feed cho cho ban be
                //Feed newFeed = new Feed();
                //newFeed.FeedID_u = Guid.NewGuid();
                //newFeed.ActiveAccountID_u = currentUser.AccountID_u;
                //newFeed.Content_s = "";
                //newFeed.CreateDate_dt = DateTime.Now;
                //newFeed.LastUpdate_dt = DateTime.Now;
                //newFeed.PassiveAccountID_u = friendRelation.AccountID_u;
                //newFeed.Type_s = EnumFeedType.ActivityMakeFriend.ToString();
                //if (Feed.Add(newFeed)== 1)
                //{
                //    FeedForFriend.AddFeedForFriend(newFeed.FeedID_u, currentUser.AccountID_u);
                //    FeedForFriend.AddFeedForFriend(newFeed.FeedID_u, friendRelation.AccountID_u);
                //}
                return "success";
            }
            else
            {
                return "unsuccess";
            }
        }

        [RequireUserLogin()]
        public string DeleteFriendRequest(string friendID)
        {
            //undone : co nen su dung delete (friendID , accountID)
            int kq = Friend.Delete(Guid.Parse(friendID), RequireUserLogin.LoginUser.AccountID_u);
            if (kq == 1)
            {
                return "success";
            }
            else
            {
                return "Có lỗi xảy ra, vui lòng thử lại sau";
            }
        }

        [RequireUserLogin()]
        public ActionResult FriendRequest()
        {
            int totalItems = 0;
            ViewBag.LstFriendRequest = Friend.SelectFriendRequestForAnAccount(RequireUserLogin.LoginUser.AccountID_u, 1, Constants.DEFAULT_NUMBER_OF_ITEMS_PERPAGE, ref totalItems);
            ViewBag.TotalItems = totalItems;
            ViewBag.CurrentPage = 1;
            return View();
        }

        [HttpPost]
        [RequireUserLogin()]
        public ActionResult FriendRequest(int page)
        {
            int totalItems = 0;
            List<Friend> lstFriendRequest = Friend.SelectFriendRequestForAnAccount(RequireUserLogin.LoginUser.AccountID_u, page, Constants.DEFAULT_NUMBER_OF_ITEMS_PERPAGE, ref totalItems);
            ViewBag.TotalItems = totalItems;
            ViewBag.CurrentPage = page;
            return PartialView("ListFriendRequest", lstFriendRequest);
        }

        [RequireUserLogin()]
        [HttpPost]
        public string MakeFriend(string friendAccountID)
        {
            //Kiem tra co yeu cau ket ban chua. Neu co thong bao dang cho ket ban
            //Undone : A,B == B,A ???
            Friend f = Friend.Single(RequireUserLogin.LoginUser.AccountID_u, Guid.Parse(friendAccountID));
            if (f != null)
            {
                return "unsuccess/2";
            }

            Account curUser = RequireUserLogin.LoginUser;
            Friend fRelation = Friend.Single(curUser.AccountID_u, Guid.Parse(friendAccountID));
            if (fRelation != null)
            {
                return "success";
            }
            else
            {
                fRelation = new Friend();
                fRelation.AccountID_u = curUser.AccountID_u;
                fRelation.FriendAccountID_u = Guid.Parse(friendAccountID);
                fRelation.CreateDate_dt = DateTime.Now;
                fRelation.Status_i = 0;
                fRelation.FriendID_u = Guid.NewGuid();
                if (Friend.MakeFriend(fRelation) > 0)
                {
                    return "success";
                }
            }
            return "Có lỗi, vui lòng thử lại sau";
        }

    }
}
